package com.halflife.comeElectricity.battery;

import com.halflife.framework.converter.MapHandler;
import com.halflife.framework.domain.Page;
import com.halflife.framework.participant.Department;
import com.halflife.framework.participant.DepartmentRepository;
import com.halflife.framework.participant.DepartmentService;
import com.halflife.framework.participant.user.UserView;
import com.halflife.shiro.utils.ShiroUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class BatteryAdminController {

    private Logger log = LoggerFactory.getLogger(this.getClass());
    @Resource
    private BatteryRepository batteryRepository;
    @Resource
    private DepartmentRepository departmentRepository;
    @Resource
    private DepartmentService departmentService;

    @RequestMapping("/battery/list")
    public String gotoList(@RequestParam(required = false) HashMap<String,Object> queryParam, Page page, HttpServletResponse response,
                           HttpServletRequest request, Model model) {
        UserView user = ShiroUtil.getUserView(request);
        MapHandler.handleMap(queryParam);
        queryParam.put("departmentId", user.getDepartmentId());
        if (page == null) {
            page = new Page();
        }
        page.setQueryParam(queryParam);
        int totalCount = batteryRepository.selectCount(queryParam);
        List<Battery> list = batteryRepository.selectListByPage(queryParam);
        page.setTotalCount(totalCount);
        page.setMode(model);
        model.addAttribute("list",list);
        model.addAttribute("queryParam",queryParam);
        return "battery/list";
    }

    @RequestMapping("/battery/operate")
    public String gotoOperate(Integer id,HttpServletResponse response,
                               HttpServletRequest request, Model model) {
        departmentService.initDepartment(request,model);
        Battery battery = new Battery();
        if (id != null) {
            battery = batteryRepository.selectByPrimaryKey(id);
        }

        model.addAttribute("battery", battery);
        return "battery/operate";
    }

    @PostMapping("/battery/save")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseEntity save(Battery battery, HttpServletRequest request,
                              HttpServletResponse response, Model model) {
        try {
            if(battery.getId()!=null){
                batteryRepository.updateByPrimaryKey(battery);
            }else {
                battery.init();
                Integer a = batteryRepository.insert(battery);
            }
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }

    @DeleteMapping("/battery/delete")
    public ResponseEntity setRiderState(Integer id, Integer state) {
        try {
            Battery battery = new Battery();
            battery.setId(id);
            battery.setValid(Boolean.FALSE);
            batteryRepository.updateByPrimaryKeySelective(battery);
        } catch (Exception ex) {
            log.error(ex.toString(),ex);
            return new ResponseEntity(1, HttpStatus.INTERNAL_SERVER_ERROR);
        }
        return new ResponseEntity(0, HttpStatus.OK);
    }
}
